<?php

namespace App\Repository\Sys;

/**
 * 一些系统操作
 */
class Curl
{

    public static function post($url, $param = array(), $timeout = 20)
    {
        if (is_array($param)) {
            $param = http_build_query($param);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false); //设定是否输出页面内容
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //连接超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //下载超时
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //强制使用IPV4协议解析域名
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        $output = curl_exec($ch);
        //$rinfo=curl_getinfo($ch);  //请求信息 $rinfo['http_code']===200
        //$error_info=curl_error($ch);
        curl_close($ch);
        return $output;
    }

    public static function get($url, $param = null, $timeout = 20)
    {
        if ($param and !empty($param)) {
            if (is_array($param)) {
                $param = http_build_query($param);
            }
            $url .= '?' . $param;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0); ////设定是否输出页面内容
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //连接超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //下载超时
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //强制使用IPV4协议解析域名
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        $output = curl_exec($ch);
        //$rinfo=curl_getinfo($ch);  //请求信息 $rinfo['http_code']===200
        //$error_info=curl_error($ch);
        curl_close($ch);
        return $output;
    }
}

